package com.healthcloud.controller;

import com.healthcloud.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.healthcloud.service.UserService;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.ShiroUtils;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by xiam on 2015/6/23.
 * Updated by ZhengL on 2015/08/12
 */
@ApiIgnore
@Api(value = "示例说明", description = "用户基本信息")
@RestController
@RequestMapping("/app/user")
public class UserApiController {

    @Autowired
    private UserService userService;

    /**
     * 修改用户密码
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @return
     */
    @ApiOperation(value = "修改用户密码", notes = "修改用户密码")
    @RequestMapping(value = "/password", method = RequestMethod.POST)
    public Object changePassword(String oldPassword, String newPassword) {
        Long userId = ShiroUtils.getUserId();
        try {
            userService.changePassword(userId, oldPassword, newPassword);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiRes(false,null,"","原密码错误");
        }
        return ApiRes.SUCCESS;
    }
    
    /**
     * 上传用户头像
     * @param mobile  手机
     * @param userImage 图片base64编码
     * @return
     */
    @ApiOperation(value = "上传用户头像", notes = "上传用户头像")
    @RequestMapping(value = "/uploadUserImage", method = RequestMethod.POST)
    public Object headPortrait(String mobile,String userImage){
        if(mobile!=null){
            mobile = mobile.trim().toLowerCase();
        }
        ApiRes jm = new ApiRes("");
    	if(StringUtils.isNotBlank(mobile)){
            SysUserEntity user = userService.findByMobile(mobile);
            user.setUserImage(userImage);
            userService.update(user);
            jm.setMsg("上传头像成功！");
            jm.setSuccess(true);
    	}else{
    		jm.setMsg("手机号为空！");
    		jm.setSuccess(false);
    	}
    	return jm;
    }
    
}
